package top.frsf.pattern.c_behavioral.c_observer.spring.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.frsf.pattern.c_behavioral.c_observer.spring.event.LogEvent;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author FRSF
 * @since 2024/9/4 23:08
 */
@RestController
@RequestMapping("/observer")
public class UserController {

    @Resource
    private ApplicationEventPublisher eventPublisher;

    @GetMapping
    public void login(String username) {
        String time = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        LogEvent debug = new LogEvent(this, time, "DEBUG", username + "Login");
        eventPublisher.publishEvent(debug);
    }
}
